package org.jeecg.modules.kaoqgz.service.impl;

import org.jeecg.modules.kaoqgz.entity.AgKaoqgz;
import org.jeecg.modules.kaoqgz.entity.AgKaoqTesgzr;
import org.jeecg.modules.kaoqgz.entity.AgKaoqJiejr;
import org.jeecg.modules.kaoqgz.mapper.AgKaoqTesgzrMapper;
import org.jeecg.modules.kaoqgz.mapper.AgKaoqJiejrMapper;
import org.jeecg.modules.kaoqgz.mapper.AgKaoqgzMapper;
import org.jeecg.modules.kaoqgz.service.IAgKaoqgzService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 考勤规则
 * @Author: jeecg-boot
 * @Date:   2020-10-13
 * @Version: V1.0
 */
@Service
public class AgKaoqgzServiceImpl extends ServiceImpl<AgKaoqgzMapper, AgKaoqgz> implements IAgKaoqgzService {

	@Autowired
	private AgKaoqgzMapper agKaoqgzMapper;
	@Autowired
	private AgKaoqTesgzrMapper agKaoqTesgzrMapper;
	@Autowired
	private AgKaoqJiejrMapper agKaoqJiejrMapper;
	
	@Override
	@Transactional
	public void saveMain(AgKaoqgz agKaoqgz, List<AgKaoqTesgzr> agKaoqTesgzrList,List<AgKaoqJiejr> agKaoqJiejrList) {
		agKaoqgzMapper.insert(agKaoqgz);
		if(agKaoqTesgzrList!=null && agKaoqTesgzrList.size()>0) {
			for(AgKaoqTesgzr entity:agKaoqTesgzrList) {
				//外键设置
				entity.setZhubId(agKaoqgz.getId());
				agKaoqTesgzrMapper.insert(entity);
			}
		}
		if(agKaoqJiejrList!=null && agKaoqJiejrList.size()>0) {
			for(AgKaoqJiejr entity:agKaoqJiejrList) {
				//外键设置
				entity.setZhubId(agKaoqgz.getId());
				agKaoqJiejrMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgKaoqgz agKaoqgz,List<AgKaoqTesgzr> agKaoqTesgzrList,List<AgKaoqJiejr> agKaoqJiejrList) {
		agKaoqgzMapper.updateById(agKaoqgz);
		
		//1.先删除子表数据
		agKaoqTesgzrMapper.deleteByMainId(agKaoqgz.getId());
		agKaoqJiejrMapper.deleteByMainId(agKaoqgz.getId());
		
		//2.子表数据重新插入
		if(agKaoqTesgzrList!=null && agKaoqTesgzrList.size()>0) {
			for(AgKaoqTesgzr entity:agKaoqTesgzrList) {
				//外键设置
				entity.setZhubId(agKaoqgz.getId());
				agKaoqTesgzrMapper.insert(entity);
			}
		}
		if(agKaoqJiejrList!=null && agKaoqJiejrList.size()>0) {
			for(AgKaoqJiejr entity:agKaoqJiejrList) {
				//外键设置
				entity.setZhubId(agKaoqgz.getId());
				agKaoqJiejrMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agKaoqTesgzrMapper.deleteByMainId(id);
		agKaoqJiejrMapper.deleteByMainId(id);
		agKaoqgzMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agKaoqTesgzrMapper.deleteByMainId(id.toString());
			agKaoqJiejrMapper.deleteByMainId(id.toString());
			agKaoqgzMapper.deleteById(id);
		}
	}
	
}
